//
// Created by hkkzzxz on 24-8-31.
//

#ifndef YUNSHI_JWT_H
#define YUNSHI_JWT_H

// JWT服务类，负责JWT的创建，解码，验证

#include <iostream>
#include <json/json.h>

#include "LogManager.h"

class JWT {
public:
    JWT() {}

    //创建JWT
    std::string create(Json::Value &payload);

    //验证JWT
    Json::Value verify(const std::string &jwt);

    //解码JWT载荷
    Json::Value decodePayload(const std::string &jwt);

private:
    std::string secretKey = "YunShi2024YunShi";

    const std::string base64_chars =
            "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
            "abcdefghijklmnopqrstuvwxyz"
            "0123456789+/";

    //对字符串进行Base64Url编码
    std::string base64UrlEncode(const std::string &input);

    //Base64Url编码转化为普通字符串
    std::string base64UrlDecode(const std::string &input);

    //创建JWT签名
    std::string createSignature(const std::string &headerBase64Url, const std::string &payloadBase64Url);

    //将字符串按照指定的分隔符分割成字符串向量
    std::vector<std::string> split(const std::string &s, char delimiter);

    //规范化载荷格式
    std::string remove_extra_chars(const std::string &str);

    LogManager *logManager = LogManager::getInstance();
    std::shared_ptr<spdlog::logger> consoleLogger = logManager->getConsoleLogger();
    std::shared_ptr<spdlog::logger> fileLogger = logManager->getFileLogger();
};


#endif //YUNSHI_JWT_H
